from rest_framework.viewsets import ViewSet, ModelViewSet, GenericViewSet
from rest_framework.mixins import CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin, \
    UpdateModelMixin
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.permissions import IsAuthenticated
from guardian.shortcuts import assign_perm
from .serializers import UserFavMovieCreateSerializer, UserFavMovieReadSerializer, UserStarCreateMovieSerializer, \
    UserStarMovieUpdateSerializer, UserStarReadMovieSerializer, UserMovieShortCommentsSerializer, \
    UserMovieShortUpdateCommentsSerializer, UserMovieShortCommentsReadSerializer
from .serializers import User
# Create your views here.
from .models import UserFavMovie
from django.contrib.auth.models import Group
from rest_framework.generics import GenericAPIView
from rest_framework.views import APIView
from .throttles import OncePerDayUserThrottle


class UserFavMovieViewSet(CreateModelMixin, DestroyModelMixin, ListModelMixin, RetrieveModelMixin, GenericViewSet):
    """
    create:
        用户给一个电影点赞
    list:
        用户查看自己点赞的电影
    retrieve:
        用户查看自己点赞电影的细节
    """
    queryset = UserFavMovieCreateSerializer.Meta.model.objects.all()
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get_queryset(self):
        return UserFavMovieCreateSerializer.Meta.model.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.action == 'create':
            return UserFavMovieCreateSerializer
        if self.action in ("list", 'retrieve'):
            return UserFavMovieReadSerializer


class UserStarMovieViewSet(CreateModelMixin, ListModelMixin, UpdateModelMixin, RetrieveModelMixin, GenericViewSet):
    """
    create:
        增加评分
    list:
        查看自己的所有评分
    retrieve:
        查看一个评分的细节
    update:
        更改评分
    """
    queryset = UserStarCreateMovieSerializer.Meta.model.objects.all()
    authentication_classes = (JSONWebTokenAuthentication,)
    permission_classes = (IsAuthenticated,)
    serializer_class = UserStarCreateMovieSerializer

    def get_queryset(self):
        return UserStarCreateMovieSerializer.Meta.model.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.action == 'create':
            return UserStarCreateMovieSerializer
        if self.action in ('update', 'partial_update'):
            return UserStarMovieUpdateSerializer
        if self.action in ("list", 'retrieve'):
            return UserStarReadMovieSerializer


class UserMovieShortCommentsViewSet(ModelViewSet):
    """
    create:
        用户增加一条对电影的评论
    list:
        查看自己的所有短评
    retrieve:
        查看自己的一条短评
    update:
        修改自己的短评信息
    destroy:
        删除自己的评论信息
    """

    serializer_class = UserMovieShortCommentsSerializer
    queryset = serializer_class.Meta.model.objects.all()
    permission_classes = (IsAuthenticated,)

    def perform_create(self, serializer):
        user = self.request._user
        group = Group.objects.get_or_create(name='site_managers')[0]
        # 在创建这篇帖子的时候,授予权限
        # 然后再在做操作的时候,查看验证有木有这个权限
        instance = serializer.save()
        # 创建一条数据,这条表示的是 这个用户有这个权限，记录==权限
        assign_perm('change_usermovieshortcomments', user, instance)
        assign_perm('delete_usermovieshortcomments', user, instance)
        assign_perm('delete_usermovieshortcomments', group, instance)

    def get_throttles(self):
        if self.action == "create":
            return [t() for t in (OncePerDayUserThrottle,)]


from rest_framework.pagination import PageNumberPagination


class LargeResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 10000


"""
查看所有短评论
"""


class MovieShortCommentsReadAllViewSet(ListModelMixin, GenericViewSet):
    """
    查看相关电影的所有评论,并且返回可以管理这条评论
    """
    serializer_class = UserMovieShortCommentsReadSerializer
    queryset = serializer_class.Meta.model.objects.all()

    pagination_class = LargeResultsSetPagination



